package com.cfpl.admin_service.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cfpl.admin_service.mapper.PesticideAcquireRecordMapper;
import com.cfpl.admin_service.mapper.PesticideTypeMapper;
import com.cfpl.admin_service.service.PesticideTypeService;
import com.cfpl.project_common.context.BaseContext;
import com.cfpl.project_common.enums.CodeEnum;
import com.cfpl.project_common.exception.BusException;
import com.cfpl.project_common.pojo.PesticideAcquireRecord;
import com.cfpl.project_common.pojo.PesticideType;
import com.cfpl.project_common.pojo.PesticideUsageRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
* @author 14720
* @description 针对表【pesticide_type(农药种类表)】的数据库操作Service实现
* @createDate 2023-10-10 11:09:53
*/
@Service
public class PesticideTypeServiceImpl extends ServiceImpl<PesticideTypeMapper, PesticideType>
    implements PesticideTypeService {

    @Autowired
    private PesticideTypeMapper pesTypeMapper;

    @Autowired
    private PesticideAcquireRecordMapper pesAcqRecordMapper;

    /**
     *  分页查询农药种类
     * @param currentPage 当前页
     * @param pageSize  每页条数
     * @param condition 查询条件 eg:根据农药种类或农药名称查询
     * @return 农药种类对象
     */
    @Override
    public Page<PesticideType> querypage(int currentPage, int pageSize, String condition) {
        LambdaQueryWrapper<PesticideType> queryWrapper = new LambdaQueryWrapper<>();
        if (condition != null) {
            queryWrapper.eq(PesticideType::getName,condition).or()
                    .eq(PesticideType::getType,condition);
        }
        return pesTypeMapper.selectPage(new Page<>(currentPage,pageSize),queryWrapper);
    }

    /**
     * 新增一条农药种类对象
     * @param pesType 农药种类对象
     */
    @Override
    public void addPesType(PesticideType pesType) {
        pesTypeMapper.insert(pesType);
    }

    /**
     * 根据id查询一条农药种类对象
     * @param id 农药种类ID
     * @return
     */
    @Override
    public PesticideType queryPesTypeById(Long id) {
        return pesTypeMapper.selectById(id);
    }

    /**
     * 删除一条农药种类对象
     * @param id 农药种类id
     */
    @Override
    public void removePesTypeById(Long id) {
        pesTypeMapper.deleteById(id);
    }
    /**
     * 更新一条农药种类对象
     * @param pesType 新的农药对象
     */

    @Override
    public void updatePesType(PesticideType pesType) {
        pesTypeMapper.updateById(pesType);
    }

    /**
     * 批量删除农药种类
     * @param Ids  农药种类对象id列表
     */
    @Override
    public void deleteBatchByIds(List<Long> Ids) {
        if (Ids == null || Ids.size() < 1) {
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        pesTypeMapper.deleteBatchIds(Ids);
    }
    /**
     * 添加领取农药记录
     * @param pesAcqRecord 记录信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPesticideAcquireRecord(PesticideAcquireRecord pesAcqRecord) {
//        领取农药数量不能小于1
        if (pesAcqRecord.getAcquireQuantity() < 1) {
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
        pesAcqRecord.setAcquireDate(new Date());
        pesAcqRecord.setAdminId(BaseContext.getCurrentUserId());
//      先减库存
        pesTypeMapper.deductPesticideStock(pesAcqRecord.getPesticideTypeId(),
                pesAcqRecord.getAcquireQuantity());
//        ,pesAcqRecord.getPesticideUsage()
//        再增加记录
        pesTypeMapper.addPesticideAcquireRecord(pesAcqRecord);

    }
    /**
     * 添加农药使用记录
     * @param pesUsaRecord 记录对象
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addPesticideUsageRecord(PesticideUsageRecord pesUsaRecord) {
//    设置时间和施农药人员
        pesUsaRecord.setUsedDate(new Date());
        pesUsaRecord.setAdminId(BaseContext.getCurrentUserId());
//        更新农药领取记录  若使用的农药数量大于领取的肥料数量，就不执行更新
        boolean flag = pesAcqRecordMapper.updatePesticideUsage(pesUsaRecord);
        System.out.println("更新使用肥料记录" + flag);
//        添加农药使用记录
        if (flag) {
            pesTypeMapper.addPesticideUsageRecord(pesUsaRecord);
        }else {
            throw new BusException(CodeEnum.PARAMETER_ERROR);
        }
    }


}




